################################################################################
### Code for "The International Criminal Court: Does it Help End Civil Wars? ###
################################################################################

# Andrew Miller, MIT
# Alex Copulsky, MIT

# Load Replication Data
load("Simmons and Danner 2009 Table 2 (by country).RData")
icc<-x

# Install Packages
#install.packages("survival")
#install.packages("simPH")
library(survival)
library(simPH)

################################################
### Correcting Turkey Ratification Miscoding ###
################################################

## Replacing Turkey Ratification Coding with Zeros
icc2 <- icc
icc2$ratification[icc2$ratification == 20101] <- 0

## High Polity Score Model
polity.high <- subset(icc2,polity_2 > 0)
mod1.hp<-coxph(Surv(time=X_t0,time2=X_t,event=fail)~ratification+
                 factor(iintensitycw5)+
                 truth_commission+
                 iaid+
                 logcinc1+
                 exec_a_military_leader_+
                 logmilper1+
                 anymilitary+
                 cluster(name),ties="breslow",model=TRUE,
               data=polity.high)
summary(mod1.hp)

## High Rule of Law Model
mod2.highrule<-coxph(Surv(time=X_t0,time2=X_t,event=fail)~ratification+
                       iintensitycw5+
                       truth_commission+
                       iaid+
                       logcinc1+
                       exec_a_military_leader_+
                       logmilper1+
                       anymilitary+
                       cluster(name),ties="breslow",
                     data=highrule)
summary(mod2.highrule)

## High Constraints Model
highconstraints <- subset(icc2,polconiii > 0)
mod3.highconstraints<-coxph(Surv(time=X_t0,time2=X_t,event=fail)~ratification+
                              iintensitycw5+
                              truth_commission+
                              iaid+
                              logcinc1+
                              exec_a_military_leader_+
                              logmilper1+
                              anymilitary+
                              cluster(name),ties="breslow",
                            data=highconstraints)
summary(mod3.highconstraints)

######################################################################
### Table 1, S&D Replication of Ratification Effect on Termination ###
######################################################################

# Model 1 (Polity)

## Polity Score > 0
polity.high <- subset(icc,polity_2 > 0)
mod1.hp<-coxph(Surv(time=X_t0,time2=X_t,event=fail)~ratification+
        iintensitycw5+
        truth_commission+
        iaid+
        logcinc1+
        exec_a_military_leader_+
        logmilper1+
        anymilitary+cluster(name),ties="breslow",model=TRUE,
        data=polity.high)
summary(mod1.hp)

## Polity Score < 1
polity.low <- subset(icc,polity_2 < 1)
mod1.lp<-coxph(Surv(time=X_t0,time2=X_t,event=fail)~ratification+
               iintensitycw5+
               truth_commission+
               iaid+
               logcinc1+
               exec_a_military_leader_+
               logmilper1+
               anymilitary+
               cluster(name),ties="breslow",
               data=polity.low)
summary(mod1.lp)

# Model 2 (Rule of Law)

## Rule of Law, High
highrule <- subset(icc,irule_of_law > -1)
mod2.highrule<-coxph(Surv(time=X_t0,time2=X_t,event=fail)~ratification+
                 iintensitycw5+
                 truth_commission+
                 iaid+
                 logcinc1+
                 exec_a_military_leader_+
                 logmilper1+
                 anymilitary+
                 cluster(name),ties="breslow",
               data=highrule)
summary(mod2.highrule)

## Rule of Law, Low
lowrule <- subset(icc,irule_of_law < -1)
mod2.lowrule<-coxph(Surv(time=X_t0,time2=X_t,event=fail)~ratification+
                       iintensitycw5+
                       truth_commission+
                       iaid+
                       logcinc1+
                       exec_a_military_leader_+
                       logmilper1+
                       anymilitary+
                       cluster(name),ties="breslow",
                     data=lowrule)
summary(mod2.lowrule)

# Model 3 (Constraints)

constraints <- na.omit
highconstraints <- subset(icc,polconiii > 0)
lowconstraints <- subset(icc,polconiii == 0)

## Contraints, High
mod3.highconstraints<-coxph(Surv(time=X_t0,time2=X_t,event=fail)~ratification+
                       iintensitycw5+
                       truth_commission+
                       iaid+
                       logcinc1+
                       exec_a_military_leader_+
                       logmilper1+
                       anymilitary+
                       cluster(name),ties="breslow",
                     data=highconstraints)
summary(mod3.highconstraints)

## Constraints, None
mod3.lowconstraints<-coxph(Surv(time=X_t0,time2=X_t,event=fail)~ratification+
                              iintensitycw5+
                              truth_commission+
                              iaid+
                              logcinc1+
                              exec_a_military_leader_+
                              logmilper1+
                              anymilitary+
                              cluster(name),ties="breslow",
                            data=lowconstraints)
summary(mod3.lowconstraints)

############################
### Sierra Leone Finding ###
############################

## Figure 1, DFBETA Plots

### Low Polity Regression
reg<-lm(fail~ratification+
                 iintensitycw5+
                 truth_commission+
                 iaid+
                 logcinc1+
                 exec_a_military_leader_+
                 logmilper1+
                 anymilitary,
               data=polity.low)
summary(reg)

### Low Polity Plot
par(mfrow=c(1,2)) 
betas<-dfbetas(reg)
mob.vec<-betas
attach(polity.low)
plot(x=betas [ ,2] , y=betas [ ,4] ,
     xlab="Ratification" , ylab="Conflict Intensity", main="Model 1 (Low Polity)" ,
     col=ifelse(betas[,2]>0.6, "darkred", "darkgray"),
     pch=19)
text(0.5,0.25,labels="Sierra Leone")

### Low Constraints Regression
reg2<-lm(fail~ratification+
          iintensitycw5+
          truth_commission+
          iaid+
          logcinc1+
          exec_a_military_leader_+
          logmilper1+
          anymilitary,
        data=lowconstraints)
summary(reg2)

# Low Constraints Plot
betas<-dfbetas(reg2)
mob.vec<-betas
plot(x=betas [ ,2] , y=betas [ ,4] ,
     xlab="Ratification" , ylab="Conflict Intensity", main="Model 3 (Low Constraints)" ,
     col=ifelse(betas[,2]>0.6, "darkred", "darkgray"),
     pch=19)
text(0.8,0.21,labels="Sierra Leone")

## Table 2, Cox Models without Sierra Leone

### Low Polity
lowp.nosl <- subset(polity.low, name != "\"Sierra Leone\"")
mod1.lp<-coxph(Surv(time=X_t0,time2=X_t,event=fail)~ratification+
                 iintensitycw5+
                 truth_commission+
                 iaid+
                 logcinc1+
                 exec_a_military_leader_+
                 logmilper1+
                 anymilitary+
                 cluster(name),ties="breslow",
               data=lowp.nosl)
summary(mod1.lp)

### Low Constraints
lowc.nosl <- subset(lowconstraints, name != "\"Sierra Leone\"")
mod3.lowconstraints.nosl<-coxph(Surv(time=X_t0,time2=X_t,event=fail)~ratification+
                             iintensitycw5+
                             truth_commission+
                             iaid+
                             logcinc1+
                             exec_a_military_leader_+
                             logmilper1+
                             anymilitary+
                             cluster(name),ties="breslow",
                           data=lowc.nosl)
summary(mod3.lowconstraints.nosl)

### Sierra Leone Violence Levels 

# Reading in UCDP Data
ged<-read.csv("UCDP-GED15.csv")

# Figure 2, Density Plot of Violence in Sierra Leone

sl<-subset(ged,country=="Sierra Leone")
par(mfrow=c(1,1)) 
plot(density(x=sl$year,y=sl$code_status), lwd=4,col="darkgray",
     main="Violent Incidents, Sierra Leone",xlab="Year",ylab="Proportion of Violence")
abline(v=1998.8333333,col="darkred",lwd=2,lty=1) # signature
abline(v=2000.75,col="darkred",lwd=2,lty=2) # ratification
abline(v=2002.4166667,col="darkred",lwd=2,lty=3) # entry into force
abline(v=1999.25,col="darkblue",lwd=2) # peace accord
legend("topleft",c("Signature","Ratification","Entry into Force", "Lome Accord"),
       col=c("darkred","darkred","darkred","darkblue"),
       lty=c(1,2,3,1),lwd=c(2,2,2,2))

########################################################
### Testing Model with Conflict Intensity as Nominal ###
########################################################

# Model 1

## Polity Score > 0
polity.high <- subset(icc,polity_2 > 0)
mod1.hp<-coxph(Surv(time=X_t0,time2=X_t,event=fail)~ratification+
                 factor(iintensitycw5)+
                 truth_commission+
                 iaid+
                 logcinc1+
                 exec_a_military_leader_+
                 logmilper1+
                 anymilitary+cluster(name),ties="breslow",model=TRUE,
               data=polity.high)
summary(mod1.hp)

## Polity Score < 1
polity.low <- subset(icc,polity_2 < 1)
mod1.lp<-coxph(Surv(time=X_t0,time2=X_t,event=fail)~ratification+
                 iintensitycw5+
                 truth_commission+
                 iaid+
                 logcinc1+
                 exec_a_military_leader_+
                 logmilper1+
                 anymilitary+
                 cluster(name),ties="breslow",
               data=polity.low)
summary(mod1.lp)

# Model 2 (Rule of Law)

## Rule of Law, High
highrule <- subset(icc,irule_of_law > -1)
mod2.highrule<-coxph(Surv(time=X_t0,time2=X_t,event=fail)~ratification+
                       factor(iintensitycw5)+
                       truth_commission+
                       iaid+
                       logcinc1+
                       exec_a_military_leader_+
                       logmilper1+
                       anymilitary+
                       cluster(name),ties="breslow",
                     data=highrule)
summary(mod2.highrule)

## Rule of Law, Low
lowrule <- subset(icc,irule_of_law < -1)
mod2.lowrule<-coxph(Surv(time=X_t0,time2=X_t,event=fail)~ratification+
                      factor(iintensitycw5)+
                      truth_commission+
                      iaid+
                      logcinc1+
                      exec_a_military_leader_+
                      logmilper1+
                      anymilitary+
                      cluster(name),ties="breslow",
                    data=lowrule)
summary(mod2.lowrule)

# Table 2, Model 3 (Constraints)

constraints <- na.omit # removing NAs

# Constraints, High
highconstraints <- subset(icc,polconiii > 0)
mod3.highconstraints<-coxph(Surv(time=X_t0,time2=X_t,event=fail)~ratification+
                              factor(iintensitycw5)+
                              truth_commission+
                              iaid+
                              logcinc1+
                              exec_a_military_leader_+
                              logmilper1+
                              anymilitary+
                              cluster(name),ties="breslow",
                            data=highconstraints)
summary(mod3.highconstraints)

# Constraints, None
lowconstraints <- subset(icc,polconiii == 0)
mod3.lowconstraints<-coxph(Surv(time=X_t0,time2=X_t,event=fail)~ratification+
                             factor(iintensitycw5)+
                             truth_commission+
                             iaid+
                             logcinc1+
                             exec_a_military_leader_+
                             logmilper1+
                             anymilitary+
                             cluster(name),ties="breslow",
                           data=lowconstraints)
summary(mod3.lowconstraints)